package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pojo.Job;
import com.example.pojo.TakersJob;
import com.example.service.TakersJobService;
import com.example.mapper.TakersJobMapper;
import org.apache.ibatis.javassist.expr.NewArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Ansenown
* @description 针对表【takers_job】的数据库操作Service实现
* @createDate 2025-04-13 16:37:17
*/
@Service
public class TakersJobServiceImpl extends ServiceImpl<TakersJobMapper, TakersJob>
    implements TakersJobService{

    @Autowired
    private TakersJobMapper takersJobMapper;

    @Override
    public List<TakersJob> getJobs(Integer useridentityfer) {

        List<TakersJob> takersJobList = takersJobMapper.selectList(
                new QueryWrapper<TakersJob>().eq("user_id",useridentityfer));

        return takersJobList;
    }

    @Override
    public int insertOne(Integer useridentityfer, String username, Integer jobId) {
        TakersJob takersJobExit = takersJobMapper.selectOne(
                new QueryWrapper<TakersJob>()
                        .eq("user_id",useridentityfer)
                        .eq("job_id",jobId)
        );
        if (takersJobExit != null){
            return 1;
        } else {
            TakersJob takersJob = new TakersJob();
            takersJob.setUserId(useridentityfer);
            takersJob.setUsername(username);
            takersJob.setJobId(jobId);
            takersJob.setAccess(0);
            int one = takersJobMapper.insert(takersJob);
            if (one>0){
                return 0;
            }
        }
        return 1;
    }

    @Override
    public List<TakersJob> getTakeUsers(List<Job> jobs) {
        List<Integer> idList = new ArrayList<>();

        for (Job job:jobs){
            idList.add(job.getId());
        }

        List<TakersJob> takersJobList = takersJobMapper.selectList(
                new QueryWrapper<TakersJob>()
                        .in("job_id",idList)
        );
        return takersJobList;
    }


    @Override
    public Integer getReceiveJobNumByJobId(Integer jobId) {
        List<TakersJob> takersJobList = takersJobMapper.selectList(
                new QueryWrapper<TakersJob>().eq("job_id",jobId)
        );
        Integer receiveJobNum = 0;
        for (TakersJob takersJob : takersJobList){
            if (takersJob.getAccess() == 1){
                receiveJobNum++;
            }
        }
        return receiveJobNum;
    }

    @Override
    public List<TakersJob> getJobPeople(List<Integer> userIdList, Integer jobId) {

        List<TakersJob> takersJobList = takersJobMapper.selectList(
                new QueryWrapper<TakersJob>()
                        .in("user_id",userIdList)
                        .eq("job_id",jobId)
        );
        return takersJobList;
    }

    @Override
    public void receiveOrRejectOne(Integer jobId, Integer userId, Integer type) {
        takersJobMapper.update(
                new LambdaUpdateWrapper<TakersJob>()
                        .set(TakersJob::getAccess,type)
                        .eq(TakersJob::getJobId,jobId)
                        .eq(TakersJob::getUserId,userId)
        );
    }
}




